<?php
/**
 * WEB端首页
 * User: lyc
 * Date: 2016/11/30
 * Time: 11:41
 */
class WebIndexController extends BaseController
{
    //首页
    public function Index(){
        #逻辑处理
        try{

            // banner数据
            $banner = SellerService::instance()->BannerIndex(3);
            //体验表
            $tiyan = ItemService::instance()->TiYanItemList();
            $tiyan->balance_amount = InvestService::instance()->ExperAllAmountNum($tiyan->auto_id,999900);                  //融资金额总数
            //个人消息
            $count = 0;
            if(!empty($_SESSION[UserEnum::USER_INFO_USER_ID])){
                $count = ApiUserService::instance()->ApiOwnmesgcount();
            }
            //新手标
            $newbieItem = ItemService::instance()->ApiNewbieItem();
//            $ItObj                  =          new stdClass();
//            $ItObj->auto_id         =   $tiyan->auto_id;
//            $ItObj->number          =   '11111111';
//            $ItObj->type            =   'TIYAN';
//            $ItObj->title           =   $tiyan->exper_name;
//            $ItObj->amount          =   '999900';
//            $ItObj->amount_collect  =   '0';
//            $ItObj->rate_loaner     =   $tiyan->expect_rate;
//            $ItObj->begin_amount    =   '6666';
//            $ItObj->status          =   '';
//            $ItObj->balance_amount  =   $tiyan->balance_amount;
//            $ItObj->deadline        =   '1';
            foreach($newbieItem as $k=>&$v){
                $v->balance_amount = number_format(($v->amount  - $v->amount_collect )/100 , 0);
                //$Item[] = $v;
            }
          //  $Item[] = $ItObj;
            $new_head = "/Web/new_heads";
            $insurance = "/Web/insurance";
            $invitation = "/Web/new_peops";

            $arr=array(
                'banner'    => $banner ,
                'Item'      => $newbieItem,
                'tiyan'     => $tiyan,
                'count'     => $count,
                'new_head'  => $new_head,
                'insurance' =>$insurance,
                'invitation'=>$invitation,
            );

            $mecount= WebUserService::instance()->WebMessageCount();//未读消息的数量
            return $this->view('webview.gpw_index')->with(["info"=>$arr,"mecount"=>$mecount]);
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode(),new stdClass());
        }

    }
    //APP新手指引
    public function Newhead(){
        return $this->view('webview.gpw_newhead');
    }
    //手机M端新手指引
    public function WebNewhead(){
        return $this->view('webview.gpw_webnewhead');
    }
    //邀请
    public function NewPeops()
    {

        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if(empty($user_id)){
            return $this->view('webview.gpw_newfriends');
        }
        //获取用户邀请码
        $user_invite_code = WebUserService::instance()->UserInfoInvite($user_id);
        //邀请人数
        $invite_count = WebUserService::instance()->InviteCount($user_id);
        //已获奖励
        $invest_amount = UserService::instance()->InviteAmount($user_id);
        return $this->view('webview.gpw_newfriends')->with(['user_invite_code'=>$user_invite_code,'invite_count'=>$invite_count,'invest_amount'=>$invest_amount]);

    }

    //邀请
    public function NewPeop()
    {
        $paramObj = new stdClass();
        $paramObj->user_id       =addslashes($this->getParam('user_id'));

        if(empty($paramObj->user_id)){
            return $this->view('webview.gpw_newfriend');
        }
        $user_id = $paramObj->user_id;

        //获取用户邀请码
        $user_invite_code = WebUserService::instance()->UserInfoInvite($user_id);
        //邀请人数
        $invite_count = WebUserService::instance()->InviteCount($user_id);
        //已获奖励
        $invest_amount = UserService::instance()->InviteAmount($user_id);

        return $this->view('webview.gpw_newfriend')->with(['user_invite_code'=>$user_invite_code,'invite_count'=>$invite_count,'invest_amount'=>$invest_amount]);

    }

    //首页
    public function Insurance(){
        return $this->view('webview.gpw_insurance');
    }

    //登陆
    public function Weblogin(){
        if (Request::isMethod('POST')) {
            #收集信息
            $paramObj             = new stdClass();
            if(!get_magic_quotes_gpc()){//放sql注入
                $paramObj = new stdClass();
                $paramObj->phones       =addslashes($this->getParam('phones'));
                $paramObj->pwd          =addslashes($this->getParam('pwd'));
            }else{
                $paramObj = new stdClass();
                $paramObj->phones       =    $this->getParam('phones');          //姓名
                $paramObj->pwd           =    $this->getParam('pwd');             //

            }
            $data= $this->getParam('webtoken');
            $token=session_get('webtoken');
            if($data== $token){
                return  WebIndexService::instance()->WebUserlogin($paramObj);
            }else{
                $mess['code'] = '2';
                $mess['mess'] = '不能重复提交';
                return $mess;
            }
        }else{
            $user_id = session_get(UserEnum::USER_INFO_USER_ID);
            if(!empty($user_id)){
                header("Location: /Web/user_accounts"); exit;
            }
            $salt  =  rand(100000, 999999);
            $salt=Sha1(md5(md5($salt)));
            Session::put('webtoken',$salt);
            return $this->view('webview.gpw_login')->with('webtoken',$salt);
        }
    }

    /**
     * 注册
     * @return mixed
     */
    public function Webregister()
    {
        return $this->view('webview.gpw_register_fir');
    }

    /**
     *忘记密码
     *author:xf
     **/
    public function WebforegetPwd(){
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->mobile               =    $this->getParam('mobile');         //手机号
            $paramObj->news_captcha             =    $this->getParam('captcha');      //验证码
            if(!empty($paramObj->mobile)&&!empty($paramObj->news_captcha))
            {
                $data = VerifyService::instance()->JsCheckNewsCaptcha($paramObj);
                $mobile=VerifyService::instance()->UserMoneyjiami($paramObj->mobile);
                if($data==1){
                    $mess['code'] = '1';
                    $mess['mess'] = '验证码正确';
                    $mess['url'] =  '/Web/web_forgetset.html?phone='.$mobile;
                    return $mess;
                }
                if($data==2){
                    $mess['code'] = '2';
                    $mess['mess'] = '验证码已过期';
                    return $mess;
                }
                if($data==3){
                    $mess['code'] = '2';
                    $mess['mess'] = '验证码错误';
                    return $mess;
                }
            }
        }else{
            return $this->view('webview.gpw_forget_pwd');
        }

    }
    /**
     *忘记密码--修改密码
     *author:xf
     **/
    public function WebforegetSetPwd(){
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->newpwd               =    $this->getParam('newpwd');       //新密码
            $paramObj->surepwd             =    $this->getParam('surepwd');      //确认密码
            $paramObj->phone             =    $this->getParam('phone');      //shoujihao
            return WebIndexService::instance()->WebforegetSetPwd($paramObj);


        }else{
            $mobile=  VerifyService::instance()->UserMoneyjiamis($this->getParam('phone'));
            return $this->view('webview.gpw_forget_setpwd')->with('mobile',$mobile);
        }

    }




    /**
     * 注册下一步
     */
    public function RegisterNext()
    {
        $t_mobile = $this->getParam('mobile');
        $t_captcha = $this->getParam('captcha');
        $mobile =  VerifyService::instance()->UserMoneyjiami($t_mobile);
        $captcha = VerifyService::instance()->UserMoneyjiami($t_captcha);
        $url = '/Web/register_sec?mobile='.$mobile.'&captcha='.$captcha;
        header("Location: $url"); exit;

    }

    /**
     * 设置密码
     * @return mixed
     */
    public function RegisterSec()
    {
        $mobile = $this->getParam('mobile');
        $captcha = $this->getParam('captcha');
        $mobile =  VerifyService::instance()->UserMoneyjiamis($mobile);
        $captcha = VerifyService::instance()->UserMoneyjiamis($captcha);
        return $this->view('webview.gpw_register_sec')->with(['mobile'=>$mobile,'captcha'=>$captcha]);
    }

    /**
     * 注册验证
     * @return mixed
     */
    public function VerifyRegister()
    {
        if (Request::isMethod('POST')) {
            #接收参数
            $paramObj = new stdClass();
            $paramObj->mobile             =       $this->getParam('mobile');                    //手机号
            $paramObj->pwd                =       $this->getParam('pwd');                       //密码
            $paramObj->captcha            =       $this->getParam('captcha');                   //短信验证码
            $paramObj->invite_phone       =       $this->getParam('invite_phone');              //推荐人手机号

            #逻辑处理
            try{
                VerifyService::instance()->WebVerifyRegister($paramObj);
                return Redirect::to('/Web/register_success.html');
            }catch(Exception $e){
                print_R($e->getMessage());
                //$this->rest->error($e->getMessage(), $e->getCode());
            }
        }
    }

    /**
     * 注册成功
     * @return mixed
     */
    public function RegisterSuccess()
    {
        return $this->view('webview.gpw_register_success');
    }

    /**
     *未登陆跳转页面
     *author:xf
     **/
    public function WebUserAccount(){
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        if(!empty($user_id)){
            header("Location: Web/user_accounts"); exit;
        }
        $weburl=session_get('weburl');
        return $this->view('webview.gpw_accountwei');
    }

    public function Tucao(){
        $paramObj = new stdClass();
        $paramObj->user_id           =   session_get(UserEnum::USER_INFO_USER_ID);      //用户ID
        $paramObj->content           = $this->getParam('content');     //内容
        $paramObj->phone           = $this->getParam('phone');     //手机号
        $info = OtherService::instance()->Tucao($paramObj);
        if($info>0){
            $rs['code'] = '1';
        }else{
            $rs['code'] = '0';
        }
        return $rs;
    }


}